function Bout = de_cast_step(Bin,d,lam1,lam2,lam3,desc_pattern)
% function Bout = de_cast_step(Bin,d,lam1,lam2,lam3,desc_pattern)
% This function does one step of the de Casteljau algorithm
% Note: This function assumes that the input arguments are all column vectors
% global desc_pattern;
m_in = size(Bin,1);
% d = degree(m_in);

%% below is the alternatable algorithms
m_out = m_in-d-1;
n = length(lam1);
indx1 = desc_pattern(1:m_out,1);  % always 1:m_out, but we place it here for simplicity
indx2 = desc_pattern(1:m_out,2);
indx3 = desc_pattern(1:m_out,3);
if size(Bin,2) == 1
   Bout = Bin(indx1)*lam1' + Bin(indx2)*lam2' + Bin(indx3)*lam3';
else
   Bout = Bin(indx1,:)*spdiags(lam1,0,n,n) + Bin(indx2,:)*spdiags(lam2,0,n,n) + ...
      Bin(indx3,:)*spdiags(lam3,0,n,n);
end

% % the former algorithm is a little faster
% m_rows = d*(d+1)/2;
% m_cols = (d+1)*(d+2)/2;
% I = (1:m_rows)';
% Id = ones(m_rows,1);
% desc = sparse(I,desc_pattern(I,1),lam1*Id,m_rows,m_cols) + ...
%         sparse(I,desc_pattern(I,2),lam2*Id,m_rows,m_cols) + ...
%         sparse(I,desc_pattern(I,3),lam3*Id,m_rows,m_cols);
% Bout = desc*Bin;